<?
class ccdsObjectHandler
{
    private $config;
    private $iClassId;

    private $_has_tables=false;
    private $_has_parameter=false;

    private $ccdSQL;
    private $idField;

    private $debug=array();

    private $ccdsFormFieldHandler;

    private $project_id;

    private $table_name;


    /* ----------------------------------------------------------------------------------------- */

    function __construct( $class_id )
    {
        $this->ccdSQL=ccdSQL::getInstance();

        $query=
            "SELECT * FROM classes LEFT JOIN extensions ON class_key_ext_id = ext_id LEFT JOIN projects ON ext_key_project_id=project_id";
        $res=$this->ccdSQL->query( $query );
        $class=$this->ccdSQL->fetch_assoc( $res );

        $this->class=$class;
        $this->iClassId=$class['class_id'];

        $this->iClassId=$class_id;

        $ObjectConfig=ccdsObjectConfig::getInstance();

        $this->ObjectConfig=$ObjectConfig;
        $this->config=$ObjectConfig->getClass( $this->iClassId );
        $this->ccdsFormFieldHandler=ccdsFormFieldHandler::getInstance();

        if ( is_array( $this->config ) )
        {
            if ( is_array( $this->config['data'] ) )
            {
                $this->_has_tables=true;
                $this->table_name=$this->config['data']['name'];

                if ( is_array( $this->config['data']['columns'] ) )
                {
                    foreach ( $this->config['data']['columns'] as $fieldname => $values )
                    {
                        if ( $values['label'] == "id" )
                        {
                            $this->idField=$fieldname;
                        }
                    }
                }
            }

            if ( isset( $this->config['parameter'] ) )
            {
                if ( is_array( $this->config['parameter'] ) )
                    $this->_has_parameter=true;
            }
        }
    }
    /* ----------------------------------------------------------------------------------------- */

    public function has_tables( )
    {
        return $this->_has_tables;
    }

    public function has_parameter( )
    {
        return $this->_has_parameter;
    }

    private function __getIDfield( )
    {
        return $this->idField;
    }

    /* ----------------------------------------------------------------------------------------- */

    private function __getTableName( )
    {
        return $this->table_name;
    }

    /* ----------------------------------------------------------------------------------------- */

    private function _parseValues( &$columns, &$aRow )
    {
        $obj_vers_id=isset( $aRow[$this->__getIDfield()] ) ? $aRow[$this->__getIDfield()] : false;

        foreach ( $columns as $fieldname => $fieldConfig )
        {
            if ( "fieldset" == $fieldname )
            {
                $aRow=array_merge( $aRow, $this->_parseValues( $fieldConfig, $aRow ) );
            }
            elseif ( "@attributes" == $fieldname ) {}
            else
            {

                $aRow[$fieldname]=$this->ccdsFormFieldHandler->getFieldValue( $fieldConfig, $fieldname,
                    $aRow[$fieldname], $obj_vers_id );
            }
        }
        return $aRow;
    }

    /* ----------------------------------------------------------------------------------------- */

    function query( $language_id, $where=false, $limit=false, $offset=false, $order=false, $pager=false,
        $below_ids=false, $is_public=true, $groupby=false, $parse_content=true )
    {
        if ( $pager != false )
        {
            if ( ( $limit > $pager ) OR ( $limit == false ) )
                $limit=$pager;
        }
        else
        {
            $pager=1;
        }

        $result=array();

        $query=
            "SELECT 
							obj1.obj_id,
							obj1.obj_parent_obj_id, 
							obj1.obj_link_obj_id,
							obj1.obj_attributes,
							obj1.obj_key_class_id,
							obj1.obj_is_navi,
							obj1.obj_created,
							clas1.*,
							tpl1.*,
							obj1.obj_key_tpl_id,
							objn1.*,
		
							obj2.obj_id as obj_id2,
							obj2.obj_parent_obj_id as obj_parent_obj_id2, 
							obj2.obj_attributes as obj_attributes2,
							clas2.class_id as class_id2,
							clas2.class_name as class_name,
							tpl2.tpl_file as tpl_file2,
							tpl2.tpl_dir as tpl_dir2,
							tpl2.tpl_id as tpl_id2,
							obj2.obj_key_tpl_id as obj_key_tpl_id2,
							objn2.obj_name_name AS obj_name_name2,
							obj2.obj_is_navi
			";

        if ( $this->_has_tables && $parse_content )
        {
            $query.="," . $this->config['data']['name'] . ".* ";
        }
        $query.="							
							FROM obj_index as obj1
							";

        if ( $this->_has_tables && $parse_content )
        {
            $query.="
								STRAIGHT_JOIN obj_versions ON (
									obj_vers_key_obj_id = IF( obj1.obj_link_obj_id > 0, obj1.obj_link_obj_id, obj1.obj_id ) 
									AND
									obj_vers_key_lang_id = " . $language_id . "
									AND 
									obj_vers_visible=1
								)
								STRAIGHT_JOIN " . $this->config['data']['name'] . " ON (
									obj_vers_id = " . $this->idField . "
								)
			";
        }

        if ( $is_public )
        {
            $query.="
								STRAIGHT_JOIN obj_index_public as objp1
								ON (
									objp1.objp_key_obj_id = obj1.obj_id
									AND
									objp1.objp_key_lang_id='" . $language_id . "'
								)
				";
        }
        $query.="								
									 LEFT JOIN
										classes as clas1 ON 
										(obj1.obj_key_class_id=clas1.class_id)
									LEFT JOIN templates tpl1 ON
										(obj1.obj_key_tpl_id=tpl1.tpl_id)
									LEFT JOIN obj_names objn1 ON (
										objn1.obj_name_key_obj_id = obj1.obj_id AND
										objn1.obj_name_key_lang_id='"
            . $language_id
            . "'
									)
		
		
								LEFT OUTER JOIN obj_index as obj2 ON (
									obj1.obj_link_obj_id = obj2.obj_id
								)
					";

        if ( $is_public )
        {
            $query.="

								LEFT JOIN obj_index_public as objp2
								ON (
									objp2.objp_key_obj_id = obj2.obj_id
									AND
									objp2.objp_key_lang_id='" . $language_id . "'
								)
				";
        }
        $query.="
									LEFT JOIN
										classes as clas2 ON 
										(obj2.obj_key_class_id=clas2.class_id)
									LEFT JOIN templates tpl2 ON
										(obj2.obj_key_tpl_id=tpl2.tpl_id)
									LEFT OUTER JOIN obj_names objn2 ON (
										objn2.obj_name_key_obj_id = obj2.obj_id AND
										objn2.obj_name_key_lang_id='" . $language_id . "'
									)
			";

        $query.="		
						WHERE 1
						AND (obj1.obj_key_class_id='" . $this->iClassId . "')
						AND 
						 obj1.obj_deleted=0
						";

        if ( is_array( $below_ids ) )
        {
            $query.=" AND obj1.obj_parent_obj_id IN (" . implode( ',', $below_ids ) . ") ";
        }

        if ( $is_navi )
        {
            $query.=" AND obj1.obj_is_navi=1 ";
        }

        if ( $is_public )
        {
            $query.=" 
									AND (
										(obj1.obj_expire=0) 
											OR  (
												obj1.obj_expire=1 
												AND obj1.obj_expire_start <= '" . time() . "'
												AND obj1.obj_expire_end >= '" . time() . "'
											)
									)								
			 ";
        }

        if ( $where != false )
        {
            $where=str_replace( " obj_", " obj1.obj_", ' ' . $where );
            $query.=" AND (" . $where . ") ";
        }

        if ( $groupby != false )
        {
            $groupby=str_replace( " obj_", " obj1.obj_", ' ' . $groupby );
            $query.=" GROUP BY " . $groupby . " ";
        }

        if ( $order == "RAND()" )
        {
            $query.=" ORDER BY RAND() ";
        }
        elseif ( $order != "" )
        {
            $order=str_replace( " obj_", " obj1.obj_", ' ' . $order );
            $query.=" ORDER BY " . $order;
        }
        else
            $query.=" ORDER BY obj1.obj_position ";

        if ( $limit != false )
        {
            $query.=" LIMIT " . $limit;
        }

        if ( $offset != false )
        {
            $query.=" OFFSET " . ( $offset * $pager );
        }

        $res=$this->ccdSQL->query( $query );

        $error=$this->ccdSQL->error();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {

            $tmp=array();

            $tmp['obj_id']=$row['obj_id'];
            $tmp['obj_parent_obj_id']=$row['obj_parent_obj_id'];
            $tmp['obj_key_class_id']=$row['obj_key_class_id'];
            $tmp['class_name']=$row['class_name'];
            $tmp['class_id']=$row['class_id'];
            $tmp['tpl_dir']=$row['tpl_dir'];

            $tmp['obj_created']=$row['obj_created'];

            $tmp['obj_is_navi']=$row['obj_is_navi'];

            $tmp['obj_key_tpl_id']=$row['obj_key_tpl_id'];
            $tmp['tpl_id']=$row['tpl_id'];
            $tmp['tpl_name']=$row['tpl_name'];
            $tmp['tpl_file']=$row['tpl_file'];
            $tmp['tpl_dir']=$row['tpl_dir'];
            $tmp['obj_name_name']=$row['obj_name_name'];
            $tmp['obj_attributes']=$row['obj_attributes'];

            if ( $row['obj_link_obj_id'] > 0 )
            {
                if ( $tmp['obj_key_tpl_id'] == 0 )
                {
                    $tmp['obj_key_tpl_id']=$row['obj_key_tpl_id2'];
                    $tmp['tpl_id']=$row['tpl_id2'];
                    $tmp['tpl_name']=$row['tpl_name2'];
                    $tmp['tpl_file']=$row['tpl_file2'];
                    $tmp['tpl_dir']=$row['tpl_dir2'];
                }
                $tmp['obj_name_name']=$row['obj_name_name2'];
                $tmp['obj_attributes']=$row['obj_attributes2'];
            }

            if ( $this->has_tables() && $parse_content )
            {
                $tmp['CONTENT']=array();

                foreach ( $this->config['data']['columns'] as $fieldname => $fieldconfig )
                {
                    $tmp['CONTENT'][$fieldname]=$row[$fieldname];
                }
                $tmp['CONTENT']=$this->_parseValues( $this->config['data']['columns'], $tmp['CONTENT'] );
            }
            $result[]=$tmp;
        }
        $this->ccdSQL->free_result( $res );
        return $result;
    }

/* ----------------------------------------------------------------------------------------- */

}
